package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 商品分页查询
     * @param page
     * @param rows
     * @param key
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>> pageQuery(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "saleable",required = false)Boolean saleable){

        return ResponseEntity.ok(this.goodsService.pageQuery(page,rows,key,saleable));
    }

    @PostMapping("goods")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO){

        this.goodsService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据spuId查询对应的spuDetail信息
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id")Long id){

        return ResponseEntity.ok(this.goodsService.querySpuDetailById(id));
    }

    /**
     * 根据spuId查询对应的sku信息
     * @param spuId
     * @return
     */
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpu(@RequestParam("id")Long spuId){
        return ResponseEntity.ok(this.goodsService.querySkuBySpu(spuId));
    }

    /**
     * 根据id集合查询sku 集合
     * @param ids id集合
     * @return sku的集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids")List<Long> ids){
        return ResponseEntity.ok(this.goodsService.querySkuByIds(ids));
    }

    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){

        this.goodsService.updateGoods(spuDTO);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> modifySaleable(
            @RequestParam("id")Long id,
            @RequestParam("saleable")Boolean saleable){

        this.goodsService.modifySaleable(id,saleable);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据spu的id查询对应的spu对象
     * @param id
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id){
        return ResponseEntity.ok(this.goodsService.querySpuById(id));
    }

    /**
     * 批量减库存
     * @param skuMap 商品id及扣减数量信息
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> skuMap){
        goodsService.deductStock(skuMap);
        // 修改库存成功，返回204
        return ResponseEntity.noContent().build();
    }
}
